#include <jni.h>
#include <stdio.h>
#include "InstanceFieldAccess.h"

JNIEXPORT void JNICALL
Java_InstanceFieldAccess_accessField(JNIEnv *env, jobject obj)
{

	jfieldID fid;
	/* store the field ID */
	jstring jstr;
	const char *str;
	/* Get a reference to obj’s class */
	jclass cls = (*env)->GetObjectClass(env, obj);
	printf("In C:\n");
	/* Look for the instance field s in cls */
	fid = (*env)->GetFieldID(env, cls, "s",
			"Ljava/lang/String;");
	if (fid == NULL) {

		return; /* failed to find the field */
	}
	/* Read the instance field s */
	jstr = (*env)->GetObjectField(env, obj, fid);
	str = (*env)->GetStringUTFChars(env, jstr, NULL);
	if (str == NULL) {

		return; /* out of memory */
	}
	printf(" c.s = \"%s\"\n", str);
	(*env)->ReleaseStringUTFChars(env, jstr, str);
	/* Create a new string and overwrite the instance field */
	jstr = (*env)->NewStringUTF(env, "123");
	if (jstr == NULL) {

		return; /* out of memory */
	}
	(*env)->SetObjectField(env, obj, fid, jstr);
}








/**
 * @brief 在加载动态链接库的时候，JVM会去尝试查找此函数，可以在此函数做一些 native函数的注册
 *
 * @retval JNI的版本
 */
/*
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved){
    printf("I'm in JNI_OnLoad \n");
    //每个java进程只有一个JavaVM
    JNIEnv *env = NULL;
    jint result = -1;
    if (vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) {
        return result;
    }
    
    static JNINativeMethod gMethods[] = {
        {
            "get",
            "()Ljava/lang/jint",
            (void*)Java_HelloWorld_get,            
        },
        {
            "set",
            "(Ljava/lang/jint)V",
            (void*)Java_HelloWorld_set,
        }
    };
	jclass clazz = (*env)->FindClass(env, "com/study/jni/HelloWorld");
    (env)->RegisterNatives(env, clazz, gMethods, NELEM(gMethods));
    return JNI_VERSION_1_6;
}
*/

